﻿using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
  public partial class EventLogControl : AdminUserControl
  {
    public EventLogControl()
    {
      InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the event log", new ActionExecuteDelegate(RefreshLog));
      actionList.Add("Copy to clipboard", "Copy the contents of the event log to the clipboard",
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actionList;
    }

    public void LoadEventLog(EventLog log)
    {
      eventLogGrid.DataSource = log;
      eventLogGrid.ClearColumns();
      eventLogGrid.AddColumn("TimeWritten", "Time");
      eventLogGrid.AddColumn("EntryType", "Type");
      eventLogGrid.AddColumn("Source");
      eventLogGrid.AddColumn("Category");
      eventLogGrid.AddColumn("Message");
      eventLogGrid.AutosizeColumns();
      eventLogGrid.AutosizeRows();
    }

    private string RefreshLog()
    {
      using (new CursorHandler())
      {
        EventLog log = (EventLog)eventLogGrid.DataSource;
        log.Refresh();
        eventLogGrid.RefreshGrid();
        OnRefreshRequired();
        return null;
      }
    }

    private string CopyToClipboard()
    {
      eventLogGrid.CopyToClipboard();
      return null;
    }
  }
}
